package com.demo.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo.entity.User;
import com.demo.repository.UserRepo;

@RestController
public class LoginController {

	@Autowired
	private UserRepo userRepo;
	
	@RequestMapping(value = "/doLogin")
	public String login(@RequestParam(value="username") String username,@RequestParam(value="password") String password,HttpSession session) {
		//判断用户名是否为空
		if (null!=username) {
	    	//判断密码是否正确
			List<User> list = userRepo.findByPhone(username);
			if ((null!=list) && (list.size()>0)) {
				if (list.get(0).getPassword().equals(password)) {
					//用户名、密码正确，正常登陆
					String type = list.get(0).getType();
					session.setAttribute("user", list.get(0));
					return "success:"+type;
				}else {
					//密码错误
				}
			}else{
				//该手机号不存在用户
				
			}
		}
		return "错误:用户名或密码错误";
	}
}
